define(['app'], function(app) {
    app.controller('EditModuleController', function($http, xContext, xDialog, $uibModalInstance) {
        var self = this;
        self.origin = self.module = '';

        var isCreated = false;

        self.init = function() {
            self.origin = self.module = xContext;
            isCreated = (self.origin == '');
        }

        self.save = function(valid) {
            if (valid) {
                if (isCreated) {
                    $http.post('/mock/module/save', { module: self.module }).then(function(response) {
                        if (response.data) {
                            $uibModalInstance.close(true);
                        }
                    });
                } else {
                    $http.post('/mock/module/rename', {
                        oldModule: self.origin,
                        newModule: self.module
                    }).then(function(response) {
                        if (response.data) {
                            $uibModalInstance.close(true);
                        } else {
                            xDialog.alert('提示信息', '您重命名的模块已存在，请更改为其它名称！');
                        }
                    });
                }
            }
        }

        self.cancel = function() {
            $uibModalInstance.dismiss();
        }
    });
});
